import request from '@/router/axios'
const scope = 'server'
const serviceType = 'BACKEND';
export const loginByUsername = (username, password, code, randomStr) => {
  const grant_type = 'password'

  return request({
    url: '/auth/oauth/token',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'post',
    params: { username, password, randomStr, code, grant_type, scope }
  })
}
export const loginByMobile = (mobile, code) => {
  const grant_type = 'mobile'

  return request({
    url: '/auth/mobile/token',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'post',
    params: { mobile,code, grant_type, scope }
  })
}
export const loginByThird = (providerId, code,username,password) => {
  const grant_type = 'third'

  return request({
    url: '/auth/social/token',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'post',
    params: {providerId, code,username,password, grant_type, scope }
  })
}

export const existSocialUser = (oauthCode, code)=>{
  return request({
    url: '/auth/login/check',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'get',
    params: {oauthCode, code, serviceType }
  })
}
export const bindSocialUser = (oauthCode,data)=>{
  console.log(serviceType,oauthCode,data);
  return request({
    url: '/auth/login/user/bind',
    headers:{
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'post',
    params:{serviceType,oauthCode},
    data:data
  })
}
export const sendLoginSmsCode = (mobile) => {
  return request({
    url: '/auth/login/sms',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'get',
    params: {mobile, serviceType}
  })
}
export const sendBindSmsCode = (mobile) => {
  return request({
    url: '/auth/login/bind/sms',
    headers: {
      isToken:false,
      //'Authorization': 'Basic cGlnOnBpZw=='
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'
    },
    method: 'get',
    params: {mobile, serviceType}
  })
}
export const refreshToken = (refresh_token) => {
  const grant_type = 'refresh_token'
  return request({
    url: '/auth/oauth/token',
    headers: {
      'isToken': false,
      //'Authorization': 'Basic cGlnOnBpZw==',
      'Authorization': 'Basic ZGV2OmFsbHJvdW5k'

    },
    method: 'post',
    params: { refresh_token, grant_type, scope }
  })
}

export const getUserInfo = () => {
  return request({
    url: '/admin/user/info',
    method: 'get'
  })
}

export const logout = () => {
  return request({
    url: '/auth/token/logout',
    method: 'delete'
  })
}
